import java.util.Stack;

public class Solution402 {
    public String removeKdigits(String num, int k) {
        Stack<Character> stack=new Stack<>();
        int time=0;
        for (char c : num.toCharArray()) {
            while (!stack.isEmpty() && c<stack.peek() && time<k){
                stack.pop();
                time++;
            }
            stack.push(c);
        }
        StringBuilder stringBuilder=new StringBuilder();
        for (int i=0;i<num.length()-k;i++){
            stringBuilder.append(stack.get(i));
        }
        while (stringBuilder.length()>1 && stringBuilder.charAt(0)=='0'){
            stringBuilder.deleteCharAt(0);
        }
        return stringBuilder.length()==0?"0":stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution402().removeKdigits("1432219",3));
    }
}
